import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import Vue from '@vitejs/plugin-vue'
import VueTsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { fileURLToPath } from 'url'

console.log()

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src')
      }
    },
    plugins: [
      Vue(),
      VueTsx(),
      AutoImport({
        imports: ['vue', 'vue-router', {
          '@renderer/utils': ['$request']
        }],
        resolvers: [ElementPlusResolver()],
        dts: 'src/types/auto-imports.d.ts',
        eslintrc: {
          enabled: true,
          filepath: fileURLToPath(new URL('./.eslintrc-auto-import.json', import.meta.url)),
          globalsPropValue: true
        }
      }),
      Components({
        resolvers: [ElementPlusResolver(),
          IconsResolver({
            prefix: 'icon', // 自动引入的Icon组件统一前缀，默认为 i，设置false为不需要前缀
            // {prefix}-{collection}-{icon} 使用组件解析器时，您必须遵循名称转换才能正确推断图标。
            // alias: { park: 'icon-park' } 集合的别名
            enabledCollections: ['ep', 'mdi', 'noto', 'mage', 'devicon', 'codicon', 'ph'] // 这是可选的，默认启用 Iconify 支持的所有集合['mdi']
          })
        ],
        dts: 'src/types/components.d.ts'
      }),
      Icons({
        autoInstall: true,
        compiler: "vue3",
      })
    ],
    server: {
      hmr: true,
      proxy: {
        '/api': {
          target: 'http://localhost:8080/api',
          rewrite: (url) => url.replace(/^\/api/, "")
        }
      }
    }
  }
})
